package logic

import (
	"context"
	"github.com/SupenBysz/gf-admin-community/sys_model"
	"github.com/SupenBysz/gf-admin-community/sys_model/sys_enum"
	"github.com/SupenBysz/gf-admin-community/sys_service"
	"github.com/SupenBysz/gf-admin-company-modules/co_interface"
	"github.com/SupenBysz/gf-admin-company-modules/co_model"
	"github.com/SupenBysz/tt-demo/api"
	"github.com/SupenBysz/tt-demo/internal/model"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/util/gconv"
	"net/http"
)

type SchoolLogic struct {
	Modules co_interface.IModules
	Dao model.IExamination
}

func NewSchoolLogic(modules co_interface.IModules) *SchoolLogic {
	return &SchoolLogic{
		Modules: modules,
		Dao:     model.NewExamination() ,
	}
}

// CreateStudent 添加学生
func (s *SchoolLogic) CreateStudent (ctx context.Context, data *api.CreateMemberReq)(resp *api.ApiRes,err error){
	resp = &api.ApiRes{}

	//调用项目一注册
	// 创建登录信息
	user, err := sys_service.SysUser().CreateUser(ctx, sys_model.UserInnerRegister{
		Username:        data.Name,
		Mobile:          gconv.String(data.Mobile),
	},
		sys_enum.UserState(sys_enum.User.State.Normal),
		sys_enum.UserType(sys_enum.User.Type.New(1,"Student")),
	)
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}

	// 调用项目二插入员工信息
	account,err:=s.Modules.Employee().CreateEmployee(ctx,&co_model.Employee{
		No:          data.No,
		Avatar:      data.Avatar,
		Name:        data.Name,
		Mobile:      data.Mobile,
		State:       0,
		UnionMainId: data.UnionUserId,
		HiredAt:     gtime.Now(),
	})
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}

	//创建返回信息

	resp.Data = struct {
		User any `json:"user"`
		Account any `json:"account"`
	}{
		User:    user,
		Account: account,
	}

	return

}



// CreateTeacher 添加老师
func (s *SchoolLogic) CreateTeacher (ctx context.Context, data *api.CreateMemberReq)(resp *api.ApiRes,err error){
	resp = &api.ApiRes{}

	//调用项目一注册
	// 创建登录信息
	newUser, err := sys_service.SysUser().CreateUser(ctx, sys_model.UserInnerRegister{
		Username:        data.Name,
		Mobile:          gconv.String(data.Mobile),
	},
		sys_enum.UserState(sys_enum.User.State.Normal),
		sys_enum.UserType(sys_enum.User.Type.New(2,"Teacher")),
	)
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}


	// 调用项目二插入员工信息
	account,err:=s.Modules.Employee().CreateEmployee(ctx,&co_model.Employee{
		No:          data.No,
		Avatar:      data.Avatar,
		Name:        data.Name,
		Mobile:      data.Mobile,
		State:       0,
		UnionMainId: 0,
		HiredAt:     gtime.Now(),
	})
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}


	//插入班级信息
	team,err:=s.Modules.Team().CreateTeam(ctx,&co_model.Team{
		Name:              data.Name + "的班级",
		OwnerEmployeeId:   account.Id,
	})
	if err != nil {
		resp.Msg = err.Error()
		resp.Code= http.StatusInternalServerError
		return
	}

	//创建返回信息

	resp.Data = struct {
		User any `json:"user"`
		Account any `json:"account"`
		Team any `json:"team"`
	}{
		User:    newUser,
		Account: account,
		Team: team,
	}

	return

}



// QueryMember 人员查询 ID
func (s *SchoolLogic) QueryMemberById (ctx context.Context, data *api.QueryMemberByIdReq)(resp *api.ApiRes,err error){
	resp = &api.ApiRes{}

	//user
	user,err:=sys_service.SysUser().GetUserDetail(ctx,data.Id)
	if err != nil {
		resp.Msg = err.Error()
		resp.Code= http.StatusInternalServerError
		return
	}

	// 调用项目二查询员工信息
	account,err:=s.Modules.Employee().GetEmployeeByName(ctx,user.Username)
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}

	//查询班级信息
	team,err:=s.Modules.Team().GetTeamByName(ctx,user.Username+"的班级")
	if err != nil {
		resp.Msg = err.Error()
		resp.Code= http.StatusInternalServerError
		return
	}

	//创建返回信息

	resp.Data = struct {
		User any `json:"user"`
		Account any `json:"account"`
		Team any `json:"team"`
	}{
		User:    user,
		Account: account,
		Team: team,
	}

	return
}